<template>
  <div class="sellWay_wrap">
    <headTop :headClass="'white_header'">
      Sale details
    </headTop>
    <div class="scroll_wrap">
      <div class="total_box">
        <div class="to_left">
          <h4>{{amount}}</h4>
          <span>Purchase amount ( CNY )</span>
        </div>
        <div class="to_right">
          <div class="img_box">
            <img :src="img" alt="Head portrait">
          </div>
          <span>{{nick}}</span>
        </div>
      </div>
      <div class="info">
        <div class="info_top">
          <div class="top_left">
            <span class="iconfont icon-ziliao"></span>
            <span class="txt">Buyer information</span>
          </div>
        </div>
        <div class="info_content">
          <div class="content_left">
            <p>
              <span class="title">Real name:</span>
              <span class="content" v-html="showName"></span>
            </p>
            <p>
              <span class="title">Turnover:</span>
              <span class="content">{{num}} CNY</span>
            </p>
          </div>
          <div class="content_right">
            <p>
              <span class="title">Contact:</span>
              <span class="content" v-html="showPhone"></span>
            </p>
            <p>
              <span class="title">Honesty value:</span>
              <span class="content level_num">{{level}}</span>
            </p>
          </div>
        </div>
      </div>
      <div class="way">
        <div class="info_top">
          <div class="top_left">
            <span class="iconfont icon-ziliao"></span>
            <span class="txt">Selection of receivables</span>
          </div>
          <!-- <div class="notice">After submission, you can check buyer's account.</div> -->
        </div>
        <ul class="way_list">
          <li class="way_item" v-for="(item, index) in typeList" :key="index" @click="selectWay(index)" v-if="item.status !==2">
            <span class="way_title">{{item.type}}</span>
            <span class="iconfont" :class="{'icon-weixuanzhong' : select !== index, 'icon-xuanzhong1' : select === index}"></span>
          </li>
        </ul>
      </div>
      <div class="sure">
        <div class="public_btn" @click="isSetPwd">Confirm the sale</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'sellWay',
  data () {
    return {
      id: Number(this.$route.query.id) || 0,
      nick: '',
      img: '',
      amount: '',
      name: '',
      num: '',
      phone: '',
      level: '',
      select: 0,
      alipayBind: 2,
      wxinBind: 2,
      bankBind: 2,
      wayId: '',
      typeList: {
        alipay: {
          type: 'Alipay',
          id: 1,
          status: 2
        },
        wechat: {
          type: 'WeChat',
          id: 2,
          status: 2
        },
        bankcard: {
          type: '',
          id: 3,
          status: 2
        }
      },
      link: '', // Order number
      jin: '',
      zhifu: '',
      countTime: ''
    };
  },
  computed: {
    showPhone () {
      return this.HidePhone(this.phone);
    },
    showName () {
      return this.name.length > 2 ? this.name.replace(/([\S]{1})\S+([\S])/, '$1**') : this.name;
    },
    passWord () {
      return this.$store.state.modal.password;
    }
  },
  methods: {
    selectWay (num) {
      this.select = num;
      this.wayId = this.typeList[this.select].id;
    },
    // Sales details interface
    getInfo () {
      this.axios.post('index/present/presenthall/sell_trading_send', {
        ids: this.id
      }).then(({data}) => {
        if (data.status === 200) {
          this.amount = data.data.point;
          this.nick = data.username;
          this.img = data.avatar;
          // Buyer information
          this.name = data.data.username;
          this.phone = data.data.mobile;
          this.num = data.data.completion_point;
          this.level = data.data.praise;
          // Bound state
          this.typeList.alipay.status = data.zhi.apliay;
          this.typeList.wechat.status = data.zhi.wechat;
          if (data.zhi.bank !== 2) {
            this.typeList.bankcard.type = data.zhi.bank.bank_name;
            this.typeList.bankcard.status = 1;
          }
          // If the judgment does not exist, delete the recirculated object and find the first index.
          if (data.zhi.apliay === 2) delete this.typeList.alipay;
          if (data.zhi.wechat === 2) delete this.typeList.wechat;
          if (data.zhi.bank === 2) delete this.typeList.bankcard;
          for (let i in this.typeList) {
            this.select = i;
            break;
          }
          this.wayId = this.typeList[this.select].id;
        } else {
          this.Toast(data.message);
        }
      });
    },
    // Determining whether to set payment password
    isSetPwd () {
      this.axios.get('index/users/pay_pass').then(({data}) => {
        if (data.status === 200) {
          this.submit();
        } else {
          this.Modal('Set transaction password', 'Transaction password is required before operation can be carried out., Please set up immediately.', ['cancel', 'To set up'], () => {
            this.$router.push('setPassword');
          });
        }
      });
    },
    // Carry out the sale
    submit () {
      this.ShowPassword(() => {
        this.axios.post('index/buyandbuy/buy_buy', {
          ids: this.id,
          type: this.wayId,
          pass: this.passWord
        }).then(({data}) => {
          if (data.status === 209) {
            this.Modal('Tips', 'Before you bind the payment method, please bind it before.', ['cancel', 'Bindings'], () => {
              this.$router.push('bindList');
            });
          } else if (data.status === 200) {
            this.Toast('Sell success');
            this.$router.replace({path: 'myOrder/sellOrder?navIndex=1'});
          } else {
            this.Toast(data.message);
          }
        });
      });
    }
  },
  mounted () {
    this.getInfo();
  }
};
</script>

<style lang="scss" scoped>
  @import '../style/public.scss';
  .sellWay_wrap{
    height: 100%;
    padding-top: 90px;
    background: #f4f5f7;
    .scroll_wrap {
      height: 100%;
      overflow-y: scroll;
      .total_box {
        height: 203px;
        background: url('./../image/pic2_02.png') no-repeat;
        background-size: cover;
        padding: 0 45px;
        color: #fff;
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
        .to_left {
          text-align: left;
        }
        .to_right {
          text-align: center;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding: 42px 0;
          span {
            color: #222222;
          }
          .img_box {
            width: 100%;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            img {
              width: 70px;
              height: 70px;
              display: block;
              border-radius: 50%;
              border: 2px solid #fff;
            }
          }
        }
        h4 {
          font-size: 48px;
          padding-top: 52px;
          font-weight: 500;
        }
        span {
          font-size: 24px;
          padding-left: 5px;
        }
      }
      .info {
        background: #fff;
        margin-bottom: 20px;
        .info_top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 87px;
          border-bottom: 1Px solid #e6e6e6;
          padding-left: 30px;
          .iconfont {
            padding-right: 10px;
            color: #5c7af3;
          }
          .txt {
            font-size: 26px;
            color: #666666;
          }
        }
        .info_content {
          height: 190px;
          padding: 0 30px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          p {
            line-height: 65px;
            .title {
              font-size: 26px;
              color: #999999;
            }
            .content {
              font-size: 26px;
              color: #222222;
              &.level_num {
                font-size: 28px;
              }
            }
          }
        }
      }
      .way {
        background: #fff;
        margin-bottom: 68px;
        .info_top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 87px;
          border-bottom: 1Px solid #e6e6e6;
          padding: 0 30px;
          .iconfont {
            padding-right: 10px;
            color: #5c7af3;
          }
          .txt {
            font-size: 26px;
            color: #666666;
          }
          .notice {
            font-size: 26px;
            color: #ff9e27;
          }
        }
        .way_list {
          .way_item {
            height: 108px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 28px;
            color: #222222;
            border-bottom: 1Px solid #e6e6e6;
            padding: 0 30px;
            .icon-weixuanzhong {
              color: #a5a5a5;
            }
            .icon-xuanzhong1 {
              color: #ffb117;
            }
          }
        }
      }
      .sure {
        text-align: center;
        margin-bottom: 90px;
        .public_btn {
          box-shadow: 0px 0px 10px 10px #f6ebd2;
        }
      }
    }
  }
</style>
